package com.geek.auth.domain;

import com.geek.auth.util.SignUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * 鉴权的领域对象，拥有令牌是否过期，令牌校验等功能
 */
@Data
@NoArgsConstructor
public class AuthToken {
    private static final long EXPIRE_INTERVAL = 10 * 60 * 1000L;
    private long timestamp;
    private String token;
    private long expire = EXPIRE_INTERVAL;

    public AuthToken(long timestamp, String token) {
        this.timestamp = timestamp;
        this.token = token;
    }

    public AuthToken(String token) {
        this.token = token;
    }

    public static AuthToken generate(Map<String, String> params, String password) {
        // 对参数进行加密
        String token = SignUtils.sign(params, password, "utf-8");
        return new AuthToken(token);
    }

    public boolean isExpired() {
        return (System.currentTimeMillis() - timestamp) > expire;
    }

    public boolean match(AuthToken clientAuthToken) {
        return clientAuthToken.getToken().equals(token);
    }
}
